# -*- coding: utf-8 -*-
"""
Created on Tue Jul 14 10:59:59 2020

@author: 81283
"""

from agent.agent import get_next_s


def goal_maze():
    s = 0  # 出发地点
    state_history = [0]  # 移动的历史记录

    while (1):  # 
        next_s = get_next_s(s)
        state_history.append(next_s)  # 

        if next_s == 8:  #
            break
        else:
            s = next_s

    return state_history

if __name__=="__main__":
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    from collections import defaultdict

    mpl.rcParams['font.sans-serif']=['SimHei']
    mpl.rcParams['axes.unicode_minus']=False 
    
    plt.grid(True, axis='y',ls=':',color='r',alpha=0.3)

    # 各次实验的步数历史
    states=[]
    
    # 共进行100000次实验
    for _ in range(100000):
        state_history = goal_maze() # 移动的历史记录
        states.append(len(state_history)-1)

    # s_list 步数及实验的次数的列表 [(步数a,次数a),(步数b,次数b),...]
    s_dict=defaultdict(int)
    for x in states:
        s_dict[x]+=1
    s_list=list(s_dict.items())
    s_list.sort(key=lambda x:x[0])
    
    plt.bar([x for x in range(len(s_list))],[x[1] for x in s_list],align='center', color='b', tick_label=[str(x[0]) for x in s_list],
            alpha=0.6, edgecolor="black")
    
    plt.xlabel('求解迷宫问题所走的步数')
    plt.ylabel('实验次数')

    plt.show()